package owg.util.editor2d;


import java.awt.Rectangle;

import owg.shallowserializer.ShallowSerializer;
import owg.shallowserializer.ShallowSerializer.Comment;
import owg.util.AdditionalParsers;
import owg.util.data.property.Property;

public class Editor2DSettings
{
	static ShallowSerializer<Editor2DSettings> serializer = new ShallowSerializer<Editor2DSettings>(Editor2DSettings.class);
	static
    {
    	AdditionalParsers.register();
    }
	private transient String path;
	
	@Comment("The bounds of the editor window, when in windowed mode.")
	public Rectangle windowedBounds =  null;
	@Comment("Whether the editor window should be fullscreen.")
	public boolean isFullscreen = false;
	
	@Comment("The current pivot mode.")
	public Property<Editor2D.Pivot> pivot = new Property<>(Editor2D.Pivot.LAST_SELECTED);    
	@Comment("Whether snapping to grid is enabled")
	public Property<Boolean> snapEnabled = new Property<>(true);  
	@Comment("Whether to snap to the northwest of a cell or to the nearest vertex")
	public Property<Boolean> nwSnap = new Property<>(true);
	@Comment("Whether to display the grid")
	public Property<Boolean> snapVisible = new Property<>(true);
	@Comment("Whether round all positions down to integers")
	public Property<Boolean> snapInteger = new Property<>(true);
	@Comment("Whether to use the snapped position for selecting objects")
	public Property<Boolean> snapSelect = new Property<>(true);
	@Comment("The width of the snapping grid")
	public Property<Integer> snapWidth = new Property<>(32);
	@Comment("The height of the snapping grid")
	public Property<Integer> snapHeight = new Property<>(32);

	@Comment("Whether scale snapping is enabled")
	public Property<Boolean> snapScaleEnabled = new Property<>(false);  
	@Comment("The granularity of the scale snapping range")
	public Property<Integer> snapScaleSize = new Property<>(1);
	
	@Comment("The size of the select-paint circle")
	public Property<Integer> paintRadius = new Property<>(32);
	
	@Comment("Whether to delete objects with left mouse button")
	public Property<Boolean> destroyPaint = new Property<>(true);
	@Comment("Whether to create objects with left mouse button")
	public Property<Boolean> createPaint = new Property<>(true);
	
	public static Editor2DSettings open(String path)
	{
		Editor2DSettings s = serializer.read(path);
		if(s.pivot.get() == null)
		{
			System.err.println("Warning: Settings said pivot mode is null...");
			s.pivot.set(Editor2D.Pivot.LAST_SELECTED);
		}
		s.path = path;
		return s;
	}
	
	public void save(Editor2D editor)
	{
		
		if(editor.getFrame() != null)
		{
			windowedBounds = editor.getFrame().getWindowedBounds();
			isFullscreen = editor.getFrame().isFullscreen();
		}
		//Note: serializer will make the dir if it doesn't exist
		serializer.write(path, this);
	}
}
